package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.FeedDTO;
import com.sky.dto.NestDTO;
import com.sky.mapper.SweetMapper;
import com.sky.result.PageResult;
import com.sky.service.SweetService;
import com.sky.vo.BannerVO;
import com.sky.vo.FeedVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@Slf4j
public class SweetServiceImpl implements SweetService {

    @Autowired
    private SweetMapper sweetMapper;

    @Override
    public List<BannerVO> getBanner(String type) {
        return sweetMapper.getBanner(type);
    }

    @Override
    public List<Map<String, Object>> getReports() {
        return sweetMapper.getReports();
    }

    @Override
    public List<Map<String, Object>> getCateGoryList(String type) {
        return sweetMapper.getCateGoryList(type);
    }

    @Override
    public PageResult getArticleList(FeedDTO feedDTO) {
        Integer pageNum = (feedDTO.getPage() != null) ? feedDTO.getPage() : 1;
        Integer pageSize = (feedDTO.getPageSize() != null) ? feedDTO.getPageSize() : 10;

        PageHelper.startPage(pageNum, pageSize);
        Page<FeedVO> page = sweetMapper.getArticleList(feedDTO);
        long total = page.getTotal();
        List<FeedVO> records = page.getResult();
        return new PageResult(total, records);
    }

    @Override
    public void setGoods(Long userId, Long articleId, Number type) {
        Integer likeStatus = sweetMapper.getLikeStatus(articleId, userId);

        if (Objects.equals(type, 1)) { // 点赞
            if (likeStatus == null) {
                // 没有记录，直接插入
                sweetMapper.setGoods(userId, articleId, 1);
                sweetMapper.updateFeeds(articleId, 1);
            } else if (Objects.equals(likeStatus, 1)) {
                // 之前取消过点赞，恢复点赞
                sweetMapper.updateFeeds(articleId, 1);
                sweetMapper.setGoods(userId, articleId, 1);
            }
        } else if (Objects.equals(type, 0)) { // 取消点赞
            if (likeStatus != null && Objects.equals(likeStatus, 0)) {
                // 只有在已点赞状态下才执行逻辑删除
                sweetMapper.updateFeeds(articleId, -1);
                sweetMapper.setGoods(userId, articleId, 0);
            }
        }

    }

    @Override
    public List<Map<String, Object>> getArticleBanner(Long articleId) {

        return sweetMapper.getArticleBanner(articleId);
    }

    @Override
    public List<Map<String, Object>> getArticleDetail(Long articleId) {
        return sweetMapper.getArticleDetail(articleId);
    }

    @Override
    public List<Map<String, Object>> getNest(Long userId) {
        List<Map<String, Object>> userInfo = sweetMapper.getUserInfo(userId);

        return userInfo;
    }

    @Override
    public int updateNestById(NestDTO nestDTO) {

        return sweetMapper.updateNestById(nestDTO);
    }

    @Override
    public int addNest(NestDTO nestDTO) {
        return sweetMapper.addNestById(nestDTO);
    }

    @Override
    public int haveNest(NestDTO nestDTO) {
        return sweetMapper.haveNest(nestDTO);
    }

    @Override
    public List<Map<String, Object>> getSelfTalklIST(Integer userId, Integer nestId) {
        return sweetMapper.getSelfTalkList(userId, nestId);
    }

    @Override
    public Map<String, Object> setLikes(Integer userId, Integer noteId) {
        Integer haveCount = sweetMapper.getLike(userId, noteId);

        if (haveCount > 0) {
            sweetMapper.updateLike(noteId);
        } else {
            sweetMapper.addLike(userId, noteId);
        }
        return null;
    }

    @Override
    public String setSelfTalk(Integer userId, String noteContent) {
        Integer nestId = sweetMapper.selectNestByUserId(userId);
        log.info("nestId:{}", nestId);
        sweetMapper.setSelfTalk(nestId, userId, noteContent);
        return null;
    }

    @Override
    public List<Map<String, Object>> getCategory(Integer nestId) {
        return  sweetMapper.getCandyCateGoryList(nestId);
    }


}
